package com.myWeb.filter;

import javax.servlet.*;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;

public class OneFilter implements Filter {


    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest)servletRequest;
        String uri = request.getRequestURI();

        if(uri.indexOf("login") != -1 || "/myWeb/".equals(uri) ||
                uri.indexOf("user/add") != -1 || uri.indexOf("user_Add") != -1){
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        javax.servlet.http.HttpSession session = request.getSession(false);

        if(null == session){
            request.getRequestDispatcher("/error.html").forward(servletRequest,servletResponse);
            return;
        }

        filterChain.doFilter(servletRequest,servletResponse);

    }
}
